package cn.signalpha.modules.shop.controller.api;

import cn.signalpha.common.constant.AppConstant;
import cn.signalpha.common.utils.Arith;
import cn.signalpha.core.boot.ctrl.BaseController;
import cn.signalpha.core.mp.support.Condition;
import cn.signalpha.core.mp.support.Query;
import cn.signalpha.core.tool.api.R;
import cn.signalpha.core.tool.support.Kv;
import cn.signalpha.core.tool.utils.BeanUtil;
import cn.signalpha.modules.com.entity.User;
import cn.signalpha.modules.com.service.UserService;
import cn.signalpha.modules.shop.service.OrderSettlePlaceService;
import cn.signalpha.modules.shop.service.PatternService;
import cn.signalpha.modules.shop.service.SellerService;
import cn.signalpha.modules.shop.vo.OrderSettlePlaceVO;
import cn.signalpha.modules.shop.vo.PlaceVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 控制器
 *
 * @author signalpha
 */
@RestController
@AllArgsConstructor
@RequestMapping(AppConstant.APPLICATION_SHOP_NAME + "/wap/place")
@Api(value = "分销员", tags = "分销员")
public class ApiPlaceController extends BaseController {

	private final UserService userService;
	private final SellerService sellerService;
	private final PatternService patternService;
	private final OrderSettlePlaceService orderSettlePlaceService;

	@GetMapping("/total")
	@ApiImplicitParams({
		@ApiImplicitParam(name="createTimeBegin", dataType = "string", value = "开始时间"),
		@ApiImplicitParam(name="createTimeEnd", dataType = "string", value = "结束时间"),
	})
	@ApiOperation(value = "推广统计", notes = "")
	public R<PlaceVO.TotalVO> total(@ApiIgnore @RequestParam Map<String, Object> params) {
		Long userId = getUser(). getUserId();
		long direct = userService.countByParams(Kv.create().setAll(params).set("parentId", userId));
		long indirect = userService.countByParams(Kv.create().setAll(params).set("grandpaId", userId));
		PlaceVO.TotalVO placeTotal = new PlaceVO.TotalVO();
		placeTotal.setUserNum(direct + indirect);
		placeTotal.setOrderNum(orderSettlePlaceService.countByParams(Kv.create().setAll(params).set("userId", userId)));
		placeTotal.setOrderAmount(orderSettlePlaceService.sumByParams(Kv.create().setAll(params).set("userId", userId)));
		placeTotal.setOrderPrice(orderSettlePlaceService.sumByParams(Kv.create().setAll(params).set("userId", userId), "pay_amount"));
		return R.data(placeTotal);
	}

	@GetMapping("/order/settle/page")
	@ApiImplicitParams({
		@ApiImplicitParam(name="search", dataType = "string", value = "订单号、用户名"),
		@ApiImplicitParam(name="settleType", dataType = "string", value = "结算类型：1直推 2间推"),
		@ApiImplicitParam(name="settleStatus", dataType = "string", value = "结算状态：0未结算 1已结算"),
		@ApiImplicitParam(name="createTimeBegin", dataType = "string", value = "开始时间"),
		@ApiImplicitParam(name="createTimeEnd", dataType = "string", value = "结束时间"),
	})
	@ApiOperation(value = "佣金记录和推广人订单", notes = "")
	public R<IPage<OrderSettlePlaceVO>> page(@ApiIgnore @RequestParam Map<String, Object> params, Query query){
		params.put("userId", getUser().getUserId().toString());
		return R.data(orderSettlePlaceService.selectOrderSettlePlacePage(Condition.getPage(query), params));
	}

	@GetMapping("/order/settle/total")
	@ApiOperation(value = "佣金记录统计", notes = "")
	public R<PlaceVO.SettleOrderVO> orderSettleTotal() {
		Long userId = getUser(). getUserId();
		PlaceVO.SettleOrderVO placeTotal = new PlaceVO.SettleOrderVO();
		placeTotal.setTotal1(orderSettlePlaceService.sumByParams(Kv.create().set("userId", userId).set("settleStatus", "0")));
		placeTotal.setTotal2(orderSettlePlaceService.sumByParams(Kv.create().set("userId", userId).set("settleStatus", "1")));
		placeTotal.setTotal(Arith.add(placeTotal.getTotal1(), placeTotal.getTotal2()));
		return R.data(placeTotal);
	}

	@GetMapping("/user/page")
	@ApiImplicitParams({
		@ApiImplicitParam(name="nickName", dataType = "string", value = "昵称"),
		@ApiImplicitParam(name="parentId", dataType = "string", value = "直推列表传"),
		@ApiImplicitParam(name="grandpaId", dataType = "string", value = "间推列表传"),
		@ApiImplicitParam(name="createTimeBegin", dataType = "string", value = "开始时间"),
		@ApiImplicitParam(name="createTimeEnd", dataType = "string", value = "结束时间"),
		@ApiImplicitParam(name="bindingTimeBegin", dataType = "string", value = "开始时间"),
		@ApiImplicitParam(name="bindingTimeEnd", dataType = "string", value = "结束时间"),
	})
	@ApiOperation(value = "下级列表", notes = "")
	public R<IPage<PlaceVO.UserVO>> userDirectPage(@ApiIgnore @RequestParam Map<String, Object> params, Query query){
		Long userId = getUser().getUserId();
		QueryWrapper<User> wrapper = userService.selectUserWrapper(params);
		IPage<User> pages = userService.page(Condition.getPage(query), wrapper);
		List<PlaceVO.UserVO> placeUserList = new ArrayList<>();
		pages.getRecords().forEach(user -> {
			PlaceVO.UserVO placeUser = Objects.requireNonNull(BeanUtil.copy(user, PlaceVO.UserVO.class));
			placeUser.setUserNum(userService.countByParams(Kv.create().set("parentId", user.getId())));
			placeUser.setOrderNum(orderSettlePlaceService.countByParams(Kv.create().setAll(params).set("userId", userId).set("orderUserId", user.getId())));
			placeUser.setOrderAmount(orderSettlePlaceService.sumByParams(Kv.create().setAll(params).set("userId", userId).set("orderUserId", user.getId())));
			placeUserList.add(placeUser);
		});
		IPage<PlaceVO.UserVO> pageVo = new Page(pages.getCurrent(), pages.getSize(), pages.getTotal());
		pageVo.setRecords(placeUserList);
		return R.data(pageVo);
	}

	@GetMapping("/order/rank/list")
	@ApiImplicitParams({
		@ApiImplicitParam(name="createTimeBegin", dataType = "string", value = "开始时间"),
		@ApiImplicitParam(name="createTimeEnd", dataType = "string", value = "结束时间"),
	})
	@ApiOperation(value = "佣金排行榜", notes = "")
	public R<List<PlaceVO.RankVO>> rankOrderList(@ApiIgnore @RequestParam Map<String, Object> params){
		return R.data(orderSettlePlaceService.selectOrderRankList(params));
	}

	@GetMapping("/user/rank/list")
	@ApiImplicitParams({
		@ApiImplicitParam(name="bindingTimeBegin", dataType = "string", value = "开始时间"),
		@ApiImplicitParam(name="bindingTimeEnd", dataType = "string", value = "结束时间"),
	})
	@ApiOperation(value = "人数排行榜", notes = "")
	public R<List<PlaceVO.RankVO>> rankUserList(@ApiIgnore @RequestParam Map<String, Object> params){
		return R.data(orderSettlePlaceService.selectUserRankList(params));
	}

}
